<?php
namespace app\admin\controller;

use app\common\controller\Admin;

class Category extends Admin
{
    //定义表单字段列表
    protected function fields($data=[])
    {
        return model('admin/Category','loglic')->fields($data);
    }
    
    //定义表单初始数据
    protected function formData()
    {
        if( $id = input('id/d',0) ){
            return \daicuo\Term::get_id($id, false);
		}
        return [];
    }

    //定义表格数据（JSON）
    //category/index/?pageNumber=1&pageSize=20&sortName=user_id&sortOrder=desc&term_tpl=index&term_tpl_=like
    protected function ajaxJson()
    {
        //查询参数
        $args = array();
        $args['cache']     = false;
        $args['type']      = 'category';
        $args['limit']     = input('pageSize/d', 50);
        $args['page']      = input('pageNumber/d', 1);
        $args['sort']      = input('get.sortName/s','term_id');
        $args['order']     = input('get.sortOrder/s','desc');
        $args['search']    = input('searchText/s','');
        //普通字段查询条件
        if($this->query['term_parent'] == 0){
            unset($this->query['term_parent']);
        }
        if( $where = DcWhereQuery(['term_parent','term_order','term_module','term_status'], 'eq', $this->query) ){
            $args['where']  = $where;
        }
        //扩展字段查询条件
        $args['meta_query'] = DcMetaQuery('term_meta', $this->query);
        //数据返回
        return DcEmpty(DcTermSelect($args),['total'=>0,'data'=>[]]);
        //数据返回
        if($infos = DcTermSelect($args) ){
            return json($infos);
        }
        return json([]);
        
        //数据返回
        return DcEmpty(DcTermSelect($args),['total'=>0,'data'=>[]]);
    }
    
	//添加(数据库)
	public function save()
    {
        config('common.validate_name', 'common/Term');
        
        config('common.validate_scene', 'save');
        
        config('common.where_slug_unique', ['term_type'=>['eq','category']]);
        
        $term_id = \daicuo\Term::save(input('post.'));
        
		if($term_id < 1){
			$this->error(\daicuo\Term::getError());
		}
        
		$this->success(lang('success'));
	}
    
    //删除(数据库)
	public function delete()
    {
		$ids = input('id/a');
		if(!$ids){
			$this->error(lang('mustIn'));
		}
        
        \daicuo\Term::delete_ids($ids);
        
        $this->success(lang('success'));
	}
	
	//修改（数据库）
	public function update()
    {
		$data = input('post.');
        if(!$data['term_id']){
            $this->error(lang('mustIn'));
        }
        
        config('common.validate_name', 'common/Term');
        
        config('common.validate_scene', 'update');
        
        config('common.where_slug_unique', ['term_type'=>['eq','category']]);
        
        $info = \daicuo\Term::update_id($data['term_id'], $data);
        
        if(is_null($info)){
            $this->error(\daicuo\Term::getError());
        }
        
        $this->success(lang('success'));
	}
    
    //快速修改状态
    public function status()
    {
        if( !$ids = input('post.id/a') ){
            $this->error(lang('errorIds'));
        }
        $data = [];
        $data['term_status'] = input('request.value/s', 'hidden');
        foreach($ids as $key=>$id){
            \daicuo\Term::update(['term_id'=>$id], $data, '');
        }
        $this->success(lang('success'));
    }
    
    //默认层级结构显示
    public function tree()
    {
        /*竖形查询
        if($this->query['sortName'] == 'tree' && $this->query['searchText']==''){
            if( count($this->query) < 7){
                return $this->tree();
            }
        }*/
        $args = array();
        $args['cache'] = false;
        $args['sort']  = 'term_id';
        $args['order'] = 'desc';
        $args['type']  = 'category';
        //$args['ids'] = '1,2,3';
        //$args['module'] = 'index';
        //$args['name'] = '动作';
        //$args['slug'] = 'dongzuo';
        //$args['search'] = 'searchwd';
        $args['paginate'] = [
            'list_rows' =>DcEmpty($this->query['pageSize'], 30),
            'page' =>DcEmpty($this->query['pageNumber'], 1),
        ];
        $list = \daicuo\Term::tree($args);
        return json($list);
    }
}